﻿@{
	ViewBag.Title = "UserIndex";
	Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="layuimini-container">
	<div class="layuimini-main">

		@* 搜索信息 *@
		<fieldset class="table-search-fieldset layui-hide" id="searchField">
			<div>
				<form class="layui-form layui-form-pane" action="">
					<div class="layui-form-item">
						<div class="layui-inline">
							<div class="layui-input-inline">
								<input type="text" name="keyword" autocomplete="off" placeholder="请输入关键词" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label" style="width:120px!important">注册日期范围</label>
							<div class="layui-input-inline">
								<input name="time" type="text" autocomplete="off" class="layui-input" id="time" placeholder="选择注册日期范围">
							</div>
						</div>
						<div class="layui-inline">
							<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
						</div>
					</div>
				</form>
			</div>
		</fieldset>
		@* 数据表格顶部按钮 *@
		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container" id="toolbar">
				@* <button id="NF-add" name="NF-add" authorize class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</button>
					<button id="NF-edit" name="NF-edit" authorize class="layui-btn layui-btn-sm layui-btn-warm layui-hide" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button> *@
				<button id="NF-delete" name="NF-delete" authorize class="layui-btn layui-btn-sm layui-btn-danger layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
			</div>
		</script>
		@* 行按钮 *@
		<script type="text/html" id="currentTableBar">
			<a id="NF-delete" authorize class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
		</script>
		@* 数据表格 *@
		<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
	</div>
</div>
<script>
	layui.use(['jquery', 'form', 'table', 'common', 'layer', 'optimizeSelectOption', 'commonTable','laydate'], function () {
		var $ = layui.jquery,
			laydate=layui.laydate,
			form = layui.form,
			table = layui.table,
			layer = layui.layer,
			commonTable = layui.commonTable,
			common = layui.common;

	// 获取14天前的日期
	var startDate = new Date();
	startDate.setDate(startDate.getDate() - 14);
	//默认初始化时间
	var defaultValue = startDate.toLocaleDateString('en-CA').replace(/-/g, '/') + ' - ' + new Date().toLocaleDateString('en-CA').replace(/-/g, '/');
		//日期时间范围
		laydate.render({
			elem: '#time'
			, type: 'date'
			, format: 'yyyy/MM/dd' //时间格式
			, range: true
			, max: 0 //最大时间显示当天
			, value: defaultValue // 最近14天
			, done: function (value, date, endDate) {
				if (value === '') {
					// 当用户点击清除按钮时，value 为空
					$(this.elem).val(''); // 强制清空输入框
				}
			}
		});
		//权限控制(js是值传递)
		currentTableBar.innerHTML = common.authorizeButtonNew(currentTableBar.innerHTML);
		toolbarDemo.innerHTML = common.authorizeButtonNew(toolbarDemo.innerHTML);
		// 数据表格
		commonTable.rendertable({
			elem: '#currentTableId',
			id: 'currentTableId',
			url: '/STZBManage/User/GetGridJson',
			filter: {
				clearFilter: false
			},
			limit:50, //默认显示20条数据
			cols: [[
				{ type: "checkbox", width: 50, fixed: 'left'},
				{ field: 'HeadImg', title: '头像', minWidth: 100, sort: true, filter: true },
				{ field: 'Account', title: '账号', minWidth: 100, sort: true , filter: true},
				{ field: 'Pwd', title: '密码', minWidth: 100, sort: true, filter: true },
				{ field: 'State', title: '状态', minWidth: 100, sort: true, filter: true,
					templet: function (d) {
						//User_State：字典分类的ID
						return top.clients.dataItems["User_State"][d.State] == undefined ? d.State : top.clients.dataItems["User_State"][d.State];
					} },
				{ field: 'CreateTime', title: '注册时间', minWidth: 100, sort: true , filter: { type: 'date[yyyy/MM/dd HH:mm:ss]' }},
				{ title: '操作', width: 80, toolbar: '#currentTableBar', align: "center", fixed: 'right' }
			]]
		});
		wcLoading.close(); //关闭加载窗
		// 监听搜索操作
		form.on('submit(data-search-btn)', function (data) {
			//执行搜索重载
			commonTable.reloadtable({
				elem: 'currentTableId',
				curr: 1, //页数
				where: { //参数
					keyword: data.field.keyword,
					stime: data.field.time.split('-')[0],
					etime: data.field.time.split('-')[1]
				}
			});
			return false;
		});
	//行点击事件监听，控制按钮显示
	var oneList = ["NF-delete"];//选择1条显示
	var morerList = ["NF-delete"];//选中1条以上显示
	//commonTable.tableRowClick("radio", "currentTableFilter", "currentTableId", oneList, morerList); //单选框
	commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", oneList, morerList); //多选框
	//toolbar监听事件
		table.on('toolbar(currentTableFilter)', function (obj) {
			var data = table.checkStatus('currentTableId').data;
			var id = data.length > 0 ?data[0].Id:null;
			 if (obj.event === 'delete') { //删除按钮
				if (data.length == 0) {
					common.modalMsg("未选中数据", "warning");
					return false;
				}
				var ids = [];
				for (var i = 0; i < data.length; i++) {
					ids.push(data[i].Id);
				}
				common.deleteForm({
					url: "/STZBManage/User/DeleteForm",
					param: { id: ids.join(',') },
					success: function () {
						common.reload('data-search-btn');
					}
				});
			}
			else if (obj.event === 'TABLE_SEARCH') { //搜索按钮
				var _that = $("#searchField");
				if (_that.hasClass("layui-hide")) {
					_that.removeClass('layui-hide');
				} else {
					_that.addClass('layui-hide');
				}
				table.resize();
			}
			return false;
		});
		//toolrow监听事件
		table.on('tool(currentTableFilter)', function (obj) {
			var id = obj.data.Id;

			if (obj.event === 'delete') { //删除按钮
				common.deleteForm({
					url: "/STZBManage/User/DeleteForm",
					param: { id: id },
					success: function () {
						obj.del();
					}
				});
			}
			return false;
		});
	});
</script>